var util = require('../../utils/util.js');
var api = require('../../utils/api.js');

//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    number: '',
    question: '',
    question_types: '',
    pic:'',
    items: [
      { name: 'A', value: '' },
      { name: 'B', value: '' },
      { name: 'C', value: '' },
      { name: 'D', value: '' }
    ],
    answer: '',
    chapter: '',
    explain: '',
    result: 0,
    display: '',
    user_answer:'',
    is_radio:true,
  },
  onLoad: function () {
    var type = wx.getStorageSync('type');
    this.data.type = type;
    var question_list_4 = wx.getStorageSync(type + '_question_list_4');
    if (!question_list_4) {
      wx.showLoading({
        mask: true,
        title: '出题中，请稍后',
      })
      this.requestQuestion(type);
    } else {
      var number = wx.getStorageSync(type + '_number_4');
      number = number ? number : 0;
      this.getQuestion(number);
    }
  },

  requestQuestion: function (type) {
    if (!wx.getStorageSync(type + '_question_list_4')) {
      api.get({
        url: 'Login/getQuestionList',
        data: {
          type: type,
          subject: 1
        },
        success: res => {
          if (res.question_list) {

            wx.setStorageSync(type + '_question_list_4', res.question_list);
            var number = wx.getStorageSync(type + '_number_4');
            number = number ? number : 0;
            this.getQuestion(number);
            wx.hideLoading();
          }
        },
      });
    }
  },
  radioChange: function (e) {
    this.data.user_answer = e.detail.value;
    this.setData({ display: 0 })
    if (this.data.question_types == 2){
      var v = e.detail.value;
      this.setData({ result: (v == this.data.answer) ? 1 : 0 });
      this.setData({ display: 1 })
    }
  },
  radioChange1: function (e) {
    var v = e.detail.value;
    this.setData({ result: (v == this.data.answer) ? 1 : 0 });
    this.setData({ display: 1 })
  },
  radioChange1:function(e){
    var v = e.detail.value;
    this.setData({ result: (v == this.data.answer) ? 1 : 0 });
    this.setData({ display: 1 })
  },
  doSure:function(){
    var v = this.data.user_answer;
    var a = this.data.answer;
    var result = 1;
    if(v.length != a.length){
      result = 0;
    }else{
      for (var i = 0; i < v.length; i++) {
        if (a.indexOf(v[i]) == -1) {
          result = 0;
          break;
        }
      }
    }

    this.setData({ result: result });
    this.setData({ display: 1 })
  },
  upQuestion: function () {
    this.setData({ result: 0, display: 0 })
    if (this.data.number < 2) return;
    this.getQuestion(this.data.number - 2);
  },
  downQuestion: function () {
    this.setData({ result: 0, display: 0 })

    this.getQuestion(this.data.number);
  },
  goIndex: function () {
    wx.navigateBack({
      delta: 1
    })
  },
  getQuestion: function (number) {
    var type = wx.getStorageSync('type');
    var question_info = wx.getStorageSync(type + '_question_list_4')[number];
    var question_types = question_info.question_types;

    this.setData({
      number: number + 1,
      question: question_info.question,
      question_types: question_info.question_types,
      answer: question_info.answer,
      chapter: question_info.chapter,
      explain: question_info.explain,
      pic:question_info.pic,
    })

    if (question_types != '2') {
      this.setData({
        items: [
          { name: 'A', value: question_info.option1 },
          { name: 'B', value: question_info.option2 },
          { name: 'C', value: question_info.option3 },
          { name: 'D', value: question_info.option4 }
        ]
      })
    }
    if (question_types == '2') {
      this.setData({
        items: [
          { name: '对', value: '正确' },
          { name: '错', value: '错误' },
        ]
      })
    }
    wx.setStorageSync(type+'_number_4', number);
  }

})
